<?php


namespace app\api\controller\v1;


use app\api\model\IdcModel;
use app\common\controller\UserBaseController;
use app\common\library\IdcChecker;
use ApiOper;

class IdcController extends UserBaseController
{
    function checkAction(){
        $this->paramlist=[
          'idcno' => 'idcno',
          'name' => 'cname'
        ];

        $this->check_params();
        $this->check_token();

        $ret = (new IdcChecker())->check_ocr($this->argv['name'],$this->argv['idcno']);
        if ($ret != 0) {
            if ($ret == 2) {
                $this->throw_myexception(X_ERROR, "找不到记录！");
            } else {
                $this->throw_myexception(X_ERROR, "检验失败！");
            }
        }

        $this->ret_resp();
    }

    function listAction() {
        $this->paramlist = [
            'idcno' => 'optidcno',
            'name' => 'optcname',
            'pno' => 'int',
            'psize' => 'int',
        ];

        $this->check_params();
        $this->check_token();

        $this->resp['list'] = (new IdcModel())->query_list($this->argv, $this->argv['pos'], $this->argv['limit']);
        if (!empty($this->resp['list'])) {
            $this->resp['total'] = count($this->resp['list']);
        } else {
            $this->resp['total'] = 0;
        }

        return $this->ret_resp();
    }

    function addAction() {
        $this->paramlist = [
            'idcno' => 'idcno',
            'name' => 'cname',
            'address' => 'string',
            'nation' => 'string',
            'remark' => 'string',
            'imgs' => 'optarray',
        ];

        $this->check_params();
        $this->check_token();

        $data = $this->argv;
        $data['auditor'] = ApiOper::instance()->userinfo()['name'];
        (new IdcModel())->add($this->argv['idcno'], $this->argv['name'], 0, $this->argv);

        return $this->ret_resp();
    }
}